from MeshData import UIMesh, TextMesh
import glfw
from AudioManager import audio_player

class ElementGroup:
    def __init__(self, buttons, static_meshes=None, enabled=True):
        self.buttons = buttons
        if static_meshes is None:
            self.static_meshes = []
        else:
            self.static_meshes = static_meshes
        self.enabled = enabled

    def mouse_move_handler(self, pos):
        if self.enabled:
            for button in self.buttons:
                button.mouse_move_handler(pos)
                # if button.on_hovered:
                #     break

    def mouse_button_down_handler(self, button_type, action):
        if self.enabled:
            if button_type == glfw.MOUSE_BUTTON_LEFT and action == glfw.PRESS:  # 鼠标左键
                for button in self.buttons:
                    button.mouse_button_down_handler()

    def draw(self):
        if self.enabled:
            for button in self.buttons:
                button.draw()
            for static_mesh in self.static_meshes:
                static_mesh.draw()


class Button:
    def __init__(self,
                 screen_position,
                 pixel_size,
                 ui_manager,
                 text="Button",
                 font_size=0.00042,
                 background_image_path="Resources/UI/button.png",
                 hovered_background_image_path="Resources/UI/button_hovered.png",
                 mesh=None,
                 callback=None
                 ):
        self.ui_manager = ui_manager
        self.on_hovered = False
        self.callback = callback
        self.background_image_path, self.hovered_background_image_path = background_image_path, hovered_background_image_path
        if mesh is None:
            self.mesh = UIMesh(
                image_path=self.background_image_path,
                screen_position=screen_position,
                pixel_size=pixel_size,
                enabled=True,
                ui_manager=self.ui_manager,
                child_mesh=[
                    TextMesh(
                        text=text,
                        screen_position=(0.0, 0.0),
                        font_scale=font_size,
                        enabled=True,
                        ui_manager=self.ui_manager
                    ),
                    UIMesh(
                        image_path=self.hovered_background_image_path,
                        screen_position=(0.0, 0.0),
                        pixel_size=pixel_size,
                        enabled=False,
                        ui_manager=self.ui_manager
                    )
                ]
            )
        else:
            self.mesh = mesh


    def in_range(self, pos):
        min_x = (self.mesh.screen_position[0] - self.mesh.pixel_size[0] / 2) * self.ui_manager.current_display[0]
        min_y = (self.mesh.screen_position[1] - self.mesh.pixel_size[1] / 2) * self.ui_manager.current_display[1]
        max_x = (self.mesh.screen_position[0] + self.mesh.pixel_size[0] / 2) * self.ui_manager.current_display[0]
        max_y = (self.mesh.screen_position[1] + self.mesh.pixel_size[1] / 2) * self.ui_manager.current_display[1]
        pos = (pos[0] - self.ui_manager.current_display[0] / 2, -pos[1] + self.ui_manager.current_display[1] / 2)
        return min_x < pos[0] < max_x and min_y < pos[1] < max_y

    def mouse_move_handler(self, pos):
        self.mesh.child_mesh[1].enabled = self.in_range(pos)
        self.on_hovered = self.mesh.child_mesh[1].enabled

    def mouse_button_down_handler(self):
        if self.on_hovered and self.callback is not None:
            audio_player.play_sound("button.click")
            self.callback()

    def draw(self):
        self.mesh.draw()


class ProgressBar:
    def __init__(self,
                 screen_position,
                 pixel_size,
                 ui_manager,
                 progress=0.0,
                 background_image_path='Resources/UI/progress_bar_bg.png',
                 fill_image_path='Resources/UI/progress_bar_fill.png'
                 ):
        self.ui_manager = ui_manager
        self.progress = progress
        self.screen_position = screen_position
        self.pixel_size = pixel_size
        self.mesh = UIMesh(
            image_path=background_image_path,
            screen_position=screen_position,
            pixel_size=pixel_size,
            enabled=True,
            ui_manager=self.ui_manager,
            child_mesh=[
                UIMesh(
                    image_path=fill_image_path,
                    screen_position=(-0.5 * (1 - self.progress) * self.pixel_size[0], 0.0),
                    pixel_size=(self.progress * self.pixel_size[0], self.pixel_size[1]),
                    enabled=True,
                    ui_manager=self.ui_manager
                )
            ]
        )

    def set_progress(self, value):
        self.progress = value
        self.mesh.child_mesh[0].screen_position = (-0.5 * (1 - self.progress) * self.pixel_size[0], 0.0)
        self.mesh.child_mesh[0].pixel_size = (self.progress * self.pixel_size[0], self.pixel_size[1])

    def draw(self):
        self.mesh.draw()
